#include "CEUtilities.h"

#include "CEMouse.h"
#include "CEGUILabel.h"
#include "CEGUIPictureBox.h"
#include "CEGUICheckBox.h"


CEGUICheckBox::CEGUICheckBox(CEGUIElementBase * parent, int id, const string& name,
    	 int offsetX, int offsetY, int width, int height, 
		const string& imageNormal, const string& imageButtonDown,
		const string& imageOver, const string& imageDisabled, const string& strText, 
		bool visible, int tabindex, bool tabenable, bool bChecked)
		:CEGUIButtonBase (parent, id, name, imageNormal, imageOver, imageButtonDown,
		 imageDisabled, strText, offsetX, offsetY, width, height, visible, tabindex, tabenable)
{
	m_bChecked = bChecked;
	m_eType = GUI_CheckBox;

	m_pPicNormal->SetUVMap(41.f/256, 109.f/256, 57.f/256, 125.f/256);
	m_pPicButtonDown->SetUVMap(41.f/256, 146.f/256, 57.f/256, 162.f/256);

	SetCheckBoxElementSize();      //The order to call these two function must
	SetCheckBoxElementPosition();  //be like this: first call the "...size"function then the "...position"function.

	_bChangeSizeTL = false;
	_bChangeSizeTM = false;
	_bChangeSizeTR = false;
	_bChangeSizeML = false;
	_bChangeSizeMR = false;
	_bChangeSizeBL = false;
	_bChangeSizeBM = false;
	_bChangeSizeBR = false;
	_bChangePos = false;
	
}

void CEGUICheckBox::SetCheckBoxElementPosition()
{
	m_pPicNormal->SetPosition(0, (m_Size.y-MINCHECKBOXHEIGHT)/2);
	m_pPicButtonDown->SetPosition(0, (m_Size.y-MINCHECKBOXHEIGHT)/2);
	m_pLabel->SetPosition(MINCHECKBOXWIDTH+INTERVAL, 0);
	m_pLabel->SetFormat(AF_VMD_LEFT);
}

void CEGUICheckBox::SetCheckBoxElementSize()
{
	if(m_Size.x < MINCHECKBOXWIDTH)
		m_Size.x = MINCHECKBOXWIDTH+INTERVAL;

	if(m_Size.y <MINCHECKBOXHEIGHT)
		m_Size.y = MINCHECKBOXHEIGHT;

	SetCheckBoxElementPosition();

	m_pPicNormal->SetSize(MINCHECKBOXWIDTH, MINCHECKBOXHEIGHT);
	m_pPicButtonDown->SetSize(MINCHECKBOXWIDTH, MINCHECKBOXHEIGHT);
	m_pLabel->SetSize(m_Size.x-MINCHECKBOXWIDTH-INTERVAL, m_Size.y);
}

bool CEGUICheckBox::CheckMousePosition()
{
	if	(CEUtil::DotInRect(Mouse::GetSingleton()->GetX(),Mouse::GetSingleton()->GetY(),
		m_pPicNormal->GetPosition().x, m_pPicNormal->GetPosition().y, m_pPicNormal->GetSize().x, m_pPicNormal->GetSize().y))
		return true;
	else
		return false;

}

bool CEGUICheckBox::IsChecked()
{
	return m_bChecked;
}

void CEGUICheckBox::SetCheck(bool b)
{
	m_bChecked = b;
	if(m_bChecked)
		SetButtonStatus(BTS_BUTTONDOWN);
	else 
		SetButtonStatus(BTS_NORMAL);

}

void CEGUICheckBox::Move(int offsetx, int offsety)
{
	CEGUIButtonBase::Move(offsetx,offsety);
	SetCheckBoxElementPosition();
}

void CEGUICheckBox::SetPosition(int offsetFromParentX, int offsetFromParentY)
{
	CEGUIButtonBase::SetPosition(offsetFromParentX, offsetFromParentY);
	SetCheckBoxElementPosition();
}

void CEGUICheckBox::SetSize(int width, int height)
{
	CEGUIButtonBase::SetSize(width, height);
	SetCheckBoxElementSize();
}



HRESULT CEGUICheckBox::Update(float deltaTime)
{
	if(this->GetButtonState() != BTS_DISABLE)
	{
		if(Mouse::GetSingleton()->IsMouseKeyDown(0, true))
		{
			if(CheckMousePosition())
			{
				m_bChecked = !m_bChecked;
			}

			if(IsChecked())
				SetButtonStatus(BTS_BUTTONDOWN);
			else 
				SetButtonStatus(BTS_NORMAL);
		}
	}

	return S_OK;
}

HRESULT CEGUICheckBox::Render()
{
	if(m_bVisible)
	{
		HRESULT r = GetPicture()->Render();
		r |= m_pLabel->Render();
		return r;
	}
	else
		return S_OK;
}


void CEGUICheckBox::SaveInScrips(ofstream &fout, int level)
{
	string blank = "";
	int temp = level;
	while (temp--) blank += "\t\t";

	fout << endl;
	fout << blank << "<element_begin>" << endl;
	fout << blank << "\t" << "<CheckBox>" << endl;

	if (m_strName != CEUtil::IntToStr(m_nID))
		fout << blank << "\t\t" << "<name>" << "\t" << m_strName << endl;
	fout << blank << "\t\t" << "<size>" << "\t" << m_Size.x << "\t" << m_Size.y << endl;
	fout << blank << "\t\t" << "<offset>" << "\t" << m_OffsetFromParent.x << "\t" << m_OffsetFromParent.y << endl;
	fout << blank << "\t\t" << "<visible>" << "\t" << m_bVisible << endl;
	fout << blank << "\t\t" << "<enable>" << "\t" << m_bEnable << endl;
	fout << blank << "\t\t" << "<level>" << "\t" << m_iShowLevel << endl;

	CEGUIPictureBox::SaveInScrips(m_pPicNormal, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicMouseOver, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicButtonDown, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicButtonDisable, fout, level+1);

	CEGUILabel::SaveInScrips(m_pLabel, fout, level+1);

	fout << blank << "<element_end>" << endl;
}



void CEGUICheckBox::SaveInScrips(CEGUICheckBox* checkbox, ofstream &fout, int level)
{
	if (checkbox)
	{
		checkbox->SaveInScrips(fout, level);
	}
	else
	{
		string blank = "";
		int temp = level;
		while (temp--) blank += "\t\t";
		fout << endl;
		fout << blank << "<element_begin>" << endl;
		fout << blank << "\t" << "<CheckBox>" << endl;
		fout << blank << "\t\t" << "<NULL>" << endl;
		fout << blank << "<element_end>" << endl;
	}
}